package nl.unimaas.micc.umsnooker.gameflow;

import java.util.logging.Level;
import java.util.logging.Logger;

import nl.unimaas.micc.umsnooker.gameflow.phases.GameInitPhase;

public class GameFlow
{
	private static final Logger logger = Logger.getLogger(GameFlow.class.getName());

	private GameState currentState;
	private GamePhase currentPhase;
	private GamePhase nextPhase;
	
	public GameFlow()
	{
		currentState = new GameState();
		currentPhase = new GameInitPhase(currentState);
	}

	public void start()
	{
		logger.log(Level.INFO,"START game flow");
		
		nextPhase = new GameInitPhase(currentState);
		
		while (nextPhase != null)
		{
			currentPhase = nextPhase;
			nextPhase = null;
			logger.log(Level.INFO,"START phase",currentPhase);
			currentPhase.start();
			currentState.getCurrentPlayer().getControllingUnit().handlePhase(currentPhase);
			while (!currentPhase.isFinished())
			{
				try
				{
					Thread.sleep(10);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
			logger.log(Level.INFO,"STOP phase",currentPhase);
			nextPhase = currentPhase.getNext();
		}
		
		logger.log(Level.INFO,"STOP game flow");
	}
	
	

	public GameState getCurrentState()
	{
		return currentState;
	}
}
